<?php
/**
 * Created by PhpStorm.
 * User: xionghao
 * Date: 2017/10/18
 * Time: 10:46
 */

namespace app\user\controller;


use app\meeting\model\MeetModel;
use app\meeting\model\MeetTougaoModel;
use cmf\controller\UserBaseController;

class BaomingController extends UserBaseController
{
    public function index()
    {
        $user = cmf_get_current_user();
        $this->assign('user', $user);
        $this->assign('more', json_decode($user['more'], true));
        $tougaoModel = new MeetTougaoModel();
        $lists = $tougaoModel->with('meet')->paginate();
        $this->assign('list', $lists);

        return $this->fetch();
    }

    public function add()
    {
       $mid = $this->request->param('mid');
       $this->assign('mid',$mid);
        return $this->fetch();
    }



    public function addPost()
    {



        $params = $this->request->param();
        $mid = $this->request->param('mid');
       $meet = MeetModel::get($mid);
       if($meet['tg_status'] != 1){
           $this->error('会议禁止投稿');
       }
        $rule = [
            'title' => 'require',
            'content' => 'require',
        ];
        $message = [
            'title.require' => '标题不能为空',
            'content.require' => '内容不能为空'
        ];

        $result_error = $this->validate($params, $rule, $message);
        if ($result_error !== true) {
            $this->error($result_error);
        } else {
            $meetTougaoModel = new MeetTougaoModel();
            $params['create_uid'] = cmf_get_current_user()['id'];

            $result = $meetTougaoModel->allowField(true)->save($params);
            if ($result) {
                $this->success('投稿成功');
            } else {
                $this->error('投稿失败');
            }
        }
    }

    public function upload()
    {
        $file = $this->request->file('file');
        $result = $file->move('.' . DS . 'upload' . DS . 'avatar' . DS);

        if ($result) {
            $avatarSaveName = str_replace('//', '/', str_replace('\\', '/', $result->getSaveName()));
            $avatar = 'file/' . $avatarSaveName;
            session('file', $avatar);

            return json_encode([
                'code' => 1,
                "msg" => "上传成功",
                "data" => ['file' => $avatar],
                "url" => '',
                'name' => $avatarSaveName
            ]);
        } else {
            return json_encode([
                'code' => 0,
                "msg" => $file->getError(),
                "data" => "",
                "url" => ''
            ]);
        }
    }
}